﻿using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;


namespace ShaderVariantCollector
{
    public class GgameShaderVariantCollector : MonoBehaviour
    {

        public bool Enable = true;
        private static bool ENABLE = true;
        
        private const string VARIANTCOLLECTION_SAVEPATH = "Assets/ResourcesAssets/Shaders/";
        
        private const string VARIANTCOLLECTION_ENDNAME = "VariantCollection.shadervariants";
        private const string VARIANTCOLLECTION_ENDNAME_TEMP = "VariantCollection_Temp.shadervariants";

        private static string lastSceneName = null;

#if UNITY_EDITOR
        private static ShaderVariantCollection currentCollection;
#endif
        private void Start()
        {
            ENABLE = Enable;
        }

        public static void EnterScene(string sceneName)
        {
#if UNITY_EDITOR
            if (!ENABLE)
            {
                return;
            }
            
            if (!string.IsNullOrEmpty(lastSceneName))
            {
                LeaveScene();
            }
            
            string path = VARIANTCOLLECTION_SAVEPATH + sceneName + VARIANTCOLLECTION_ENDNAME;
            currentCollection = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(path);
            ShaderUtils.ClearCurrentShaderVariantCollection();

            lastSceneName = sceneName;
#endif

        }

        public static void LeaveScene()
        {
#if UNITY_EDITOR
            if (!ENABLE)
            {
                return;
            }

            if (string.IsNullOrEmpty(lastSceneName))
            {
                return;
            }

            bool changed = false;
            
            string path = VARIANTCOLLECTION_SAVEPATH + lastSceneName + VARIANTCOLLECTION_ENDNAME;
            if (currentCollection)
            {
                // 得到并集
                string tmpPath = VARIANTCOLLECTION_SAVEPATH + lastSceneName + VARIANTCOLLECTION_ENDNAME_TEMP;
                ShaderUtils.SaveCurrentShaderVariantCollection(tmpPath);
                var svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(tmpPath);
                
                
                var newVariants = ShaderVariantCollectionHelper.ExtractData(svc);
                //var oldVariants = ShaderVariantCollectionHelper.ExtractData(currentCollection);
                
                foreach (var variants in newVariants.Values)
                {
                    foreach (var sv in variants)
                    {
                        try
                        {
                            ShaderVariantCollection.ShaderVariant shaderVariant = new ShaderVariantCollection.ShaderVariant(sv.shader,sv.passType,sv.keywords);
                            if (!currentCollection.Contains(shaderVariant))
                            {
                                currentCollection.Add(shaderVariant);
                                changed = true;
                            }

                        }
                        catch (Exception e)
                        {
                            // 说明此变体不属于当前创建的shader的指定pass类型
                            // 走到这里，一般都因unity搜集的变体属于依赖项
                        }
                    }
                }

                AssetDatabase.DeleteAsset(tmpPath);
            }
            else
            {
                changed = true;
                ShaderUtils.SaveCurrentShaderVariantCollection(path);
            }

            if (changed)
            {
                AssetDatabase.SaveAssets();
            }
#endif

        }


#if UNITY_EDITOR
        private void OnApplicationQuit()
        {
            if (!ENABLE)
            {
                return;
            }
            
            if (!string.IsNullOrEmpty(lastSceneName))
            {
                LeaveScene();
            }
        }





        [MenuItem("Tools/ShaderVariantCollector/ClearCollection")]
        public static void ClearCollection()
        {
            ClearCollection("PveVariantCollection");
            ClearCollection("WorldVariantCollection");
            ClearCollection("CityVariantCollection");
        }

        private static void ClearCollection(string name)
        {
            var path = string.Format("Assets/ResourcesAssets/Shaders/{0}.shadervariants", name);
            AssetDatabase.DeleteAsset(path);
            AssetDatabase.CreateAsset(new ShaderVariantCollection(),path);
        }
#endif  
        
        
        
    }
}
